/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.rm.entidades.jpa;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;

import org.rm.entidades.ExParcial;
import org.rm.entidades.ExParcial;
import org.rm.entidades.jpa.exceptions.NonexistentEntityException;

/**
 *
 * @author ffbeltran
 */
public class ExParcialController {

    public static void create(ExParcial exParcial) {
        EntityManager em = null;
        try {
            em = JpaController.getEntityManager();
            em.getTransaction().begin();
            em.persist(exParcial);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public static void edit(ExParcial exParcial) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = JpaController.getEntityManager();
            em.getTransaction().begin();
            exParcial = em.merge(exParcial);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = exParcial.getId();
                if (findExParcial(id) == null) {
                    throw new NonexistentEntityException("The exParcial with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public static void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = JpaController.getEntityManager();
            em.getTransaction().begin();
            ExParcial exParcial;
            try {
            	exParcial = em.getReference(ExParcial.class, id);
            	exParcial.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The exParcial with id " + id + " no longer exists.", enfe);
            }
            em.remove(exParcial);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public static List<ExParcial> findExParcialEntities() {
        return findExParcialEntities(true, -1, -1);
    }

    public static List<ExParcial> findExParcialEntities(int maxResults, int firstResult) {
        return findExParcialEntities(false, maxResults, firstResult);
    }

    private static List<ExParcial> findExParcialEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = JpaController.getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from ExParcial as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public static List<ExParcial> findExParcialEntities(int idPartida) {
        EntityManager em = JpaController.getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from ExParcial as o where o.idPartida = :idP");
            q.setParameter("idP", idPartida);
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public static ExParcial findExParcial(Integer id) {
        EntityManager em = JpaController.getEntityManager();
        try {
            return em.find(ExParcial.class, id);
        } finally {
            em.close();
        }
    }

    public static int getExParcialCount() {
        EntityManager em = JpaController.getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from ExParcial as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
